package com.xh.audit.controller.project;

import com.xh.audit.entity.auditdocument.AuditDocuments;
import com.xh.audit.service.AuditDocumentsService;
import com.xh.audit.vo.webform.AuditDocumentQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 审计文书 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/audit-documents")
@RequiredArgsConstructor
@Validated
public class AuditDocumentsController {
    private final AuditDocumentsService auditDocumentsService;

    /**
     * 审计文书保存 要记录档案
     *
     * @param tosave
     * @return
     */
    @PostMapping("/save")
    public R<AuditDocuments> save(@RequestBody
                                  @Validated
                                  AuditDocuments tosave) {
        // boolean saveFlag = auditDocumentsService.saveOrUpdate(tosave);

        boolean saveFlag = auditDocumentsService.saveAuditDocuments(tosave);

        if (saveFlag) {
            return R.success(auditDocumentsService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }


    /**
     * 通过id查
     *
     * @param queryByProjectid
     * @return
     */
    @PostMapping("/queryByProject")
    public R<List<AuditDocuments>> queryByProject(@RequestBody
                                                  @Validated
                                                  @NotNull(message = "id不能空")
                                                  AuditDocumentQuery.QueryByProjectid queryByProjectid) {

        List<AuditDocuments> list = auditDocumentsService.list(queryByProjectid.queryWrapperBuild());
        return R.success(list);
    }

    /**
     * List<Integer> 集合验证 必须在类上也加 @Validated
     * 文书删除 同步删除档案
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public R<AuditDocuments> del(@RequestBody
                                 @Validated
                                 @NotEmpty(message = "ids 集合不能空")
                                 List<Integer> ids) {
        auditDocumentsService.del(ids);
        return R.success("删除成功");
    }
}
